package service;
import java.util.List;





import javax.annotation.Resource;

import mapper.SysFuncRepository;
import mapper.SysRoleFuncRepository;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import common.BaseService;
import entity.SysFunc;
import entity.SysRoleFunc;

@Service
@Import(BaseService.class)
public class SysRoleFuncService  extends BaseService<SysRoleFunc,SysRoleFuncRepository>{
	
	private static Logger logger = Logger.getLogger(SysRoleFuncService.class);  

	@Autowired
   private SysRoleFuncRepository SysRoleFuncRepository;
	 
    @RequestMapping("save")
    public  String save(SysRoleFunc SysRoleFunc){
    	if((SysRoleFunc.getSeqid()==null || SysRoleFunc.getSeqid()==0L)){
    		SysRoleFuncRepository.insert(SysRoleFunc);
		}else{
			//save方法表示当实体存在的时候就更新，不存在则插入。但是插入的效率低于insert
			logger.info(SysRoleFuncRepository.updateById(SysRoleFunc));
		}
       return "ok";
    }
    
    @RequestMapping("save")
    public  String saveByRoleIdAndFuncStr(SysRoleFunc sysRoleFunc,long roleId){
		String [] funcIdStr = sysRoleFunc.getFuncIds().split(",");
		List<SysRoleFunc> sysRoleFuncList = SysRoleFuncRepository.sysRoleFuncByRoleId(roleId);
		for(SysRoleFunc sysCurRoleFunc:sysRoleFuncList){
			SysRoleFuncRepository.deleteForId(sysCurRoleFunc.getSeqid());
		}
    	for(String funcStr:funcIdStr){
    		long funcId = Long.parseLong(funcStr);
    		sysRoleFunc.setFuncId(funcId);
    		sysRoleFunc.setRoleId(roleId);
    		SysRoleFunc sysRoleFuncCur = SysRoleFuncRepository.sysRoleFuncByRoleIdAndFuncId(roleId, funcId);
    		if((sysRoleFuncCur==null)){
        		SysRoleFuncRepository.insert(sysRoleFunc);
    		}else{
    			//save方法表示当实体存在的时候就更新，不存在则插入。但是插入的效率低于insert
    			logger.info(SysRoleFuncRepository.updateById(sysRoleFunc));
    		}
    		
    	}
       return "ok";
    }
    
    @RequestMapping("insert")
    public  String insert(SysRoleFunc SysRoleFunc){
    	SysRoleFuncRepository.insert(SysRoleFunc);
       return "ok";
    }
   
    @RequestMapping("find")
    public List<SysRoleFunc> find(){
    	List<SysRoleFunc> SysRoleFuncList = SysRoleFuncRepository.all();
       return SysRoleFuncList;
       
    }
   
    @RequestMapping("findByName")
    public SysRoleFunc findByName(){
       return SysRoleFuncRepository.findRoleByName("张三");
    }
    
    @RequestMapping("findById")
    public SysRoleFunc findById(long seqid){
    	SysRoleFunc SysRoleFunc = SysRoleFuncRepository.findById(seqid);
       return SysRoleFunc;
    }
    
    @RequestMapping("findByRoleId")
    public List<SysRoleFunc> findByRoleId(long roleId){
    	List<SysRoleFunc> SysRoleFuncList = SysRoleFuncRepository.sysRoleFuncByRoleId(roleId);
       return SysRoleFuncList;
    }
    
    @RequestMapping("findByRoleIdAndFuncId")
    public SysRoleFunc findByRoleIdAndFuncId(long roleId,long funcId){
    	SysRoleFunc SysRoleFuncList = SysRoleFuncRepository.sysRoleFuncByRoleIdAndFuncId(roleId,funcId);
       return SysRoleFuncList;
    }
    
    @RequestMapping("deleteByIds")
    public void deleteByIds(String [] seqids){
    	for(int i=0;i<seqids.length;i++){
    		SysRoleFuncRepository.deleteForId(Long.parseLong(seqids[i]));
//			 Query query = new Query();  
//			 query.addCriteria(Criteria.where("_id").is(seqids[i]));  
//    		 mongoTemplate.remove(query, SysRoleFunc.class);
    	}
    }
	

	
    
}

